clear all
set more off, perm

/* Directories */

global orig_data_thecb "E:\"
global orig_data "E:\"
global orig_data_tea "E:\"

global work_dir  "E:\"
global ui_dir "E:\"
global logfiles "E:\"
global dofiles "E:\"
global results "E:\"


use $ui_dir/pre_analysis,replace

*Creation of variables 

sort id2 subject
  
 gen vent_st_prank_ma_all= vent_st_prank_all if subject==1
 by id2: replace vent_st_prank_ma_all=vent_st_prank_ma_all[_n-1]  if vent_st_prank_ma_all==.
 
 gen vent_st_prank_re_all= vent_st_prank_all if subject==2
 by id2: replace vent_st_prank_re_all=vent_st_prank_re_all[_n+1]  if vent_st_prank_re_all==.
 
 gen vent_cl_prank_ma_all= vent_cl_prank_all if subject==1
 by id2: replace vent_cl_prank_ma_all=vent_cl_prank_ma_all[_n-1] if vent_cl_prank_ma_all==.

 gen vent_cl_prank_re_all= vent_cl_prank_all if subject==2
 by id2: replace vent_cl_prank_re_all=vent_cl_prank_re_all[_n+1] if vent_cl_prank_re_all==.

 
*Inverse Hyperbolic Sine
 gen ihs_mean_real_wage0_1518=log(mean_real_wage0_1518+sqrt((mean_real_wage0_1518^2)+1))
 
 foreach x in real_wage0_15 real_wage0_16 real_wage0_17 real_wage0_18{
	gen ihs_`x'=log(`x'+sqrt((`x'^2)+1))
 }

egen mean_real_wage_1518=rowmean(real_wage_15 real_wage_16 real_wage_17 real_wage_18) 
gen mean_inlabor_1518=rowmean(in_labor_15 in_labor_16 in_labor_17 in_labor_18)
 
gen st_prank_3_sq_all=st_prank_3_all*st_prank_3_all
gen st_prank_3_cu_all=st_prank_3_all*st_prank_3_all*st_prank_3_all
gen st_prank_3_qu_all=st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all
gen st_prank_3_ve_all=st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all
gen st_prank_3_se_all=st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all*st_prank_3_all


sort uni_code
by uni_code: egen uni_admin=mean(st_prank_3_all)
gen uni_admin_No_College=uni_admin
replace uni_admin=. if uni_code==.
label var uni_admin "Average 8th Grade StPct of Uni"
label var uni_admin_No_College "Average 8th Grade StPct of Uni inc Not going to college"

gen uni_code_temp=college_type if uni_code!=.
replace uni_code_temp=uni_code if college_type==2
egen uni_major=group(uni_code_temp major_code)

sort uni_major
by uni_major: egen uni_major_admin=mean(st_prank_3_all)

encode campus_3, gen(sch_3)

gen st_perc8_missing=state_perc_8==.
gen ap_math= ap_calcab>0 |ap_calcbc>0

graph set window fontface "Times New Roman"

compress

*School distribution characteristics (by subject)
sort campus_3 subject
by campus_3 subject: egen school_mean=mean(st_prank_3_all)
by campus_3  subject: egen school_sd=sd(st_prank_3_all)

xtile school_mean_quartile=school_mean if cl_prank_3_all!=. ,n(4)
xtile school_sd_quartile=school_sd if cl_prank_3_all!=. ,n(4)

xtile school_mean_decile=school_mean if cl_prank_3_all!=. ,n(10)
xtile school_sd_decile=school_sd if cl_prank_3_all!=. ,n(10)

*Class distribution characteristics
sort y3_sch_coh_sub
by y3_sch_coh_sub: egen class_mean=mean(st_prank_3_all)
by y3_sch_coh_sub: egen class_sd=sd(st_prank_3_all)

xtile class_mean_quartile=class_mean if cl_prank_3_all!=. ,n(4)
xtile class_sd_quartile=class_sd if cl_prank_3_all!=. ,n(4)

xtile class_sd_decile=class_sd if cl_prank_3_all!=. ,n(10)
xtile class_size_decile=num_class_ontime if cl_prank_3_all!=. ,n(10)


*Interactions 
egen school_mean_var=group(school_mean_quartile school_sd_quartile)
egen cell_school_ventile_group=group(school_mean_quartile school_sd_quartile vent_st_prank_all)

egen sd10_school_ventile_group=group(school_sd_decile vent_st_prank_all)
egen sd4_school_ventile_group=group(school_sd_quartile vent_st_prank_all)
egen mean4_school_ventile_group=group(school_mean_quartile vent_st_prank_all)
egen mean10_school_ventile_group=group(school_mean_decile vent_st_prank_all)


egen class_mean_var=group(class_mean_quartile class_sd_quartile)
egen cell_class_ventile_group=group(class_mean_quartile class_sd_quartile vent_st_prank_all)

egen sd10_class_ventile_group=group(class_sd_decile vent_st_prank_all)
egen sd4_class_ventile_group=group(class_sd_quartile vent_st_prank_all)
egen mean4_class_ventile_group=group(class_mean_quartile vent_st_prank_all)

tab ethnic,gen(ethnic_dum)

save $ui_dir/analysis,replace


use $ui_dir/analysis,replace
cd "$results"

global controls male fsme_dum esl_dum  ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5

char vent_cl_prank_ontime[omit] 10
char vent_st_prank_ontime[omit] 10

char vent_cl_prank_all[omit] 10
char vent_st_prank_all[omit] 10

char ethnic_dum[omit] 1


********************************************************************************
*Appendix Table 1 Summary Stats**********
********************************************************************************

cap file close myfile
cd "$results"
file open myfile using summarystats.csv, write text replace
file write myfile ",Mean, Standard Deviation, Observations" _n

set more off 
file write myfile "Demographics" _n
foreach x in male fsme_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5 {
su `x'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "3rd Grade Achievement" _n
foreach x in st_prank_3_all st_pmrank_3_all st_prrank_3_all cl_prank_3_all  cl_pmrank_3_all cl_prrank_3_all num_class_3_all num_cohort_3_all {
su `x'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "School Outcomes" _n
foreach x in repeat3rd repeat4th state_perc_8 grad_graduated_hs_1 evergrad_hs evergrad_hs_dist ap_math  ap_sci ap_eng {
su `x'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "College Outcomes" _n
foreach x in any_college both_college  enr4yr_any3years enr2yr_any3years STEM_major  grad4in4y grad4in6y grad4in8y uni_admin uni_admin_No_College{
su `x'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "Labor Outcomes" _n
foreach x in   in_labor_15 real_wage0_15  ihs_real_wage0_15  real_wage_15 in_labor_18 real_wage0_18 real_wage_18 ln_mean_real_wage0_1518 ihs_real_wage0_18   mean_inlabor_1518 mean_real_wage0_1518 ihs_mean_real_wage0_1518 mean_real_wage_1518 {
su `x'
file write myfile %6.0f ("`x'") ","  %12.3f (r(mean)) "," %12.3f (r(sd)) "," %12.0f (r(N)) "," _n
}

cap file close myfile 



********************************************************************************
*Summary Stats For Balanced Panel 
********************************************************************************

cap file close myfile
cd "$results"
file open myfile using Bal_summarystats.csv, write text replace
file write myfile ",Mean, Standard Deviation, Observations" _n

set more off 
file write myfile "Demographics" _n
foreach x in male fsme_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5 {
su `x' if year_3<2002
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "3rd Grade Achievement" _n
foreach x in st_prank_3_all st_pmrank_3_all st_prrank_3_all cl_prank_3_all  cl_pmrank_3_all cl_prrank_3_all num_class_3_all num_cohort_3_all {
su `x' if year_3<2002
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "School Outcomes" _n
foreach x in repeat3rd repeat4th state_perc_8 grad_graduated_hs_1 evergrad_hs evergrad_hs_dist ap_math  ap_sci ap_eng {
su `x' if year_3<2002
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "College Outcomes" _n
foreach x in any_college both_college  enr4yr_any3years enr2yr_any3years STEM_major  grad4in4y grad4in6y grad4in8y uni_admin uni_admin_No_College{
su `x' if year_3<2002
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "Labor Outcomes" _n
foreach x in   in_labor_15 real_wage0_15  ihs_real_wage0_15  real_wage_15 in_labor_18 real_wage0_18 real_wage_18 ln_mean_real_wage0_1518 ihs_real_wage0_18   mean_inlabor_1518 mean_real_wage0_1518 ihs_mean_real_wage0_1518 mean_real_wage_1518 {
su `x' if year_3<2002
file write myfile %6.0f ("`x'") ","  %12.3f (r(mean)) "," %12.3f (r(sd)) "," %12.0f (r(N)) "," _n
}

cap file close myfile 




************************************
******Summary Stats By DisType******
************************************

cap file close myfile
cd "$results"
file open myfile using summarystats_dist.csv, write text replace
file write myfile ",Mean, Standard Deviation, Observations" _n

forvalues t=1/16{
set more off 
file write myfile "Class Distriution Type `t'"  _n
file write myfile "Demographics" _n
foreach x in male fsme_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5 {
su `x' if class_mean_var==`t'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "3rd Grade Achievement" _n
foreach x in st_prank_3_all st_pmrank_3_all st_prrank_3_all cl_prank_3_all  cl_pmrank_3_all cl_prrank_3_all num_class_3_all num_cohort_3_all {
su `x' if class_mean_var==`t'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "School Outcomes" _n
foreach x in repeat3rd repeat4th state_perc_8 grad_graduated_hs_1 evergrad_hs evergrad_hs_dist ap_math  ap_sci ap_eng {
su `x' if class_mean_var==`t'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "College Outcomes" _n
foreach x in any_college both_college  enr4yr_any3years enr2yr_any3years STEM_major  grad4in4y grad4in6y grad4in8y uni_admin uni_admin_No_College{
su `x' if class_mean_var==`t'
file write myfile %6.0f ("`x'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
}
file write myfile "Labor Outcomes" _n
foreach x in   in_labor_15 real_wage0_15  ihs_real_wage0_15  real_wage_15 in_labor_18 real_wage0_18 real_wage_18 ln_mean_real_wage0_1518 ihs_real_wage0_18   mean_inlabor_1518 mean_real_wage0_1518 ihs_mean_real_wage0_1518 mean_real_wage_1518 {
su `x' if class_mean_var==`t'
file write myfile %6.0f ("`x'") ","  %12.3f (r(mean)) "," %12.3f (r(sd)) "," %12.0f (r(N)) "," _n
}

}
cap file close myfile 



********************************************************************************
*APPENDIX FIGURE 4
*Summary stats of people at different points of the class distributions
foreach x in 10 25 50 75 90 {
by y3_sch_coh_sub: egen class_pct`x'= pctile(st_prank_3_all) , p(`x')
}

foreach x in min max {
by y3_sch_coh_sub: egen class_pct`x'= `x'(st_prank_3_all)
}

file open myfile using summarystats_JeffDist2.csv, write text replace
file write myfile ",Mean, Standard Deviation, Observations" _n

foreach x in min 10 25 50 75 90 max{
file write myfile "Class Score of these students at percentile  `x'"  _n
	foreach v in male fsme_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5 {
		
		sum class_pct`x' if `v'==1
		file write myfile %6.0f ("`v'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n

		sum class_pct`x' if  `v'==0
		file write myfile %6.0f ("Non-`v'") ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
		}
}
cap file close myfile 

********************************************************************************



	
************************************
*************Balancing**************
************************************


********************************************************************************
*Linear Balance (Table 1) and Outcomes (Appendix Table 3) by funcitonal form 
********************************************************************************

*Defined above
*egen sd10_school_ventile_group=group(school_sd_decile vent_st_prank_all)
*egen sd4_school_ventile_group=group(school_sd_quartile vent_st_prank_all)
*egen mean4_school_ventile_group=group(school_mean_quartile vent_st_prank_all)
*egen mean10_school_ventile_group=group(school_mean_decile vent_st_prank_all)
*egen cell_school_ventile_group=group(school_mean_quartile school_sd_quartile vent_st_prank_all)

foreach x in  sd10_school sd4_school mean10_school mean4_school cell_school{
capture erase "$results/Robustness_Outcomes_Interact_None.csv"
capture erase "$results/Robustness_Balance_Interact_None.csv"
capture erase "$results/Robustness_Outcomes_Interact_`x'.csv"
capture erase "$results/Robustness_Balance_Interact_`x'.csv"
}

*Appendix Table 3
foreach y in   repeat3rd state_perc_8 evergrad_hs  any_college grad4in8y ln_mean_real_wage0_1518 {
	eststo `y': reghdfe `y' cl_prank_3_all ib10.vent_st_prank_all $controls, absorb(y3_sch_coh_sub) cluster(sch_3)
	esttab using "$results/Robustness_Outcomes_Interact_None.csv", keep(cl_prank_3_all ) nobra b(%12.3f) se(%12.3f) append mlabels(,titles)
	estimates clear	
}

foreach x in  sd10_school sd4_school mean10_school mean4_school cell_school{

	foreach y in   repeat3rd state_perc_8 evergrad_hs  any_college grad4in8y ln_mean_real_wage0_1518 {
		eststo `y': reghdfe `y' cl_prank_3_all $controls, absorb(y3_sch_coh_sub `x'_ventile_group) cluster(sch_3)
		esttab using "$results/Robustness_Outcomes_Interact_`x'.csv", keep(cl_prank_3_all ) nobra b(%12.3f) se(%12.3f) append mlabels(,titles)
		estimates clear	
	}
}

*Table 1
foreach y in   fsme_dum male ethnic_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5{
	eststo `y': reghdfe `y' cl_prank_3_all ib10.vent_st_prank_all , absorb(y3_sch_coh_sub) cluster(sch_3)
	esttab using "$results/Robustness_Balance_Interact_None.csv", keep(cl_prank_3_all ) nobra b(%12.3f) se(%12.3f) append mlabels(,titles)
	estimates clear	
}

	
foreach x in sd10_school sd4_school mean10_school mean4_school cell_school {

	foreach y in  fsme_dum male ethnic_dum esl_dum ethnic_dum1 ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5 {
	eststo `y': reghdfe `y' cl_prank_3_all , absorb(y3_sch_coh_sub `x'_ventile_group) cluster(sch_3)
	esttab using "$results/Robustness_Balance_Interact_`x'.csv", keep(cl_prank_3_all ) nobra b(%12.3f) se(%12.3f) append mlabels(,titles)
	estimates clear	
	}
	
}



************************************
********Main Estimates**************
************************************

********************************************************************************
*Figures 2 3 4, Appendix Figure 6 
********************************************************************************


foreach x in repeat3rd repeat4th state_perc_8 st_perc8_missing grad_graduated_hs_1 evergrad_hs evergrad_hs_dist ap_math ap_math  ap_sci ap_eng any_college both_college  enr4yr_any3years enr2yr_any3years STEM_major  grad4in4y grad4in6y grad4in8y  in_labor_15 real_wage0_15  ihs_real_wage0_15  real_wage_15 in_labor_18 real_wage0_18 real_wage_18 ln_mean_real_wage0_1518 ihs_real_wage0_18   mean_inlabor_1518 mean_real_wage0_1518 ihs_mean_real_wage0_1518 mean_real_wage_1518 {
	estimates clear
	
	eststo:reghdfe `x'   cl_prank_3_all  $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	*cluster(sch_3_all)
		
	eststo:reghdfe `x'   ib10.vent_cl_prank_all $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	*cluster(sch_3)
	estimates store `x'
	
	coefplot ,keep(*.vent_cl_prank_all )  vertical yline(0, lwidth(vthin)) ysc(r(-.04 0.06)) ylabel(#6) xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) coeflabel(1.vent_cl_prank_all="0-5"  2.vent_cl_prank_all=" " 3.vent_cl_prank_all=" " 4.vent_cl_prank_all=" " 5.vent_cl_prank_all=" " 6.vent_cl_prank_all=" " 7.vent_cl_prank_all=" " 8.vent_cl_prank_all=" " 9.vent_cl_prank_all=" " 10.vent_cl_prank_all="45-50" 11.vent_cl_prank_all=" " 12.vent_cl_prank_all=" " 13.vent_cl_prank_all=" " 14.vent_cl_prank_all=" " 15.vent_cl_prank_all=" " 16.vent_cl_prank_all=" " 17.vent_cl_prank_all=" " 18.vent_cl_prank_all=" " 19.vent_cl_prank_all=" " 20.vent_cl_prank_all="95-100")  xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white)  mcolor(black) ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)
	graph save $results\graph_`x',  replace

	esttab using "$results/Table`x'.csv", keep(cl_prank_3_all *vent_cl_prank_all) nobra b(%12.3f) se(%12.3f) mtitles("Linear" "Non Linear") replace
	}	

*Seperate for AP for graphs 
foreach x in ap_math ap_sci ap_eng{
	estimates clear
	
	eststo:reghdfe `x'  cl_prank_3_all  $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
			
	eststo:reghdfe `x'   ib10.vent_cl_prank_all $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	
	estimates store `x'
	
	coefplot ,keep(*.vent_cl_prank_all )  vertical yline(0, lwidth(vthin)) ysc(r(-.04 0.06)) ylabel(#6) xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) coeflabel(1.vent_cl_prank_all="0-5"  2.vent_cl_prank_all=" " 3.vent_cl_prank_all=" " 4.vent_cl_prank_all=" " 5.vent_cl_prank_all=" " 6.vent_cl_prank_all=" " 7.vent_cl_prank_all=" " 8.vent_cl_prank_all=" " 9.vent_cl_prank_all=" " 10.vent_cl_prank_all="45-50" 11.vent_cl_prank_all=" " 12.vent_cl_prank_all=" " 13.vent_cl_prank_all=" " 14.vent_cl_prank_all=" " 15.vent_cl_prank_all=" " 16.vent_cl_prank_all=" " 17.vent_cl_prank_all=" " 18.vent_cl_prank_all=" " 19.vent_cl_prank_all=" " 20.vent_cl_prank_all="95-100")  xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white)  mcolor(black) ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)
	graph save $results\graph_`x', asis replace

	esttab using "$results/Table`x'.csv", keep(cl_prank_3_all *vent_cl_prank_all) nobra b(%12.3f) se(%12.3f) mtitles("Linear" "Non Linear") replace
	}	
	
************************************
*************By subject*************
************************************
egen cell_ventile_ma_group=group(school_mean_quartile school_sd_quartile vent_st_prank_ma_all)
egen cell_ventile_re_group=group(school_mean_quartile school_sd_quartile vent_st_prank_re_all)
	
foreach x in repeat3rd state_perc_8 any_college both_college enr4yr_any3years enr2yr_any3years  grad4in6y STEM_major mean_real_wage_1518  ln_mean_real_wage0_1518 mean_real_wage0_1518 ihs_mean_real_wage0_1518 { 

estimates clear
eststo Maths:reghdfe `x'  ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group) cluster(sch_3)

rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all
 eststo Reading:reghdfe `x'  ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group ) cluster(sch_3)

esttab using "$results/Subject_Table_`x'.csv", keep(*.vent_cl_prank_ma_all)  b(%12.3f) se(%12.3f)  nobra mtitles("Math" "Reading") replace

rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all

 coefplot (Maths, keep(*.vent_cl_prank_ma_all )  mcolor(gs7) ciopts(lcol(gs7) lwidth(vthin)) msize(vsmall) msymbol(D) ) (Reading, keep(*.vent_cl_prank_ma_all)  ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)  mcolor(black) )   ,baselevel vertical yline(0, lwidth(vthin))  xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash))  coeflabel(1.vent_cl_prank_ma_all="0-5"  2.vent_cl_prank_ma_all=" " 3.vent_cl_prank_ma_all=" " 4.vent_cl_prank_ma_all=" " 5.vent_cl_prank_ma_all=" " 6.vent_cl_prank_ma_all=" " 7.vent_cl_prank_ma_all=" " 8.vent_cl_prank_ma_all=" " 9.vent_cl_prank_ma_all=" " 10.vent_cl_prank_ma_all="45-50" 11.vent_cl_prank_ma_all=" " 12.vent_cl_prank_ma_all=" " 13.vent_cl_prank_ma_all=" " 14.vent_cl_prank_ma_all=" " 15.vent_cl_prank_ma_all=" " 16.vent_cl_prank_ma_all=" " 17.vent_cl_prank_ma_all=" " 18.vent_cl_prank_ma_all=" " 19.vent_cl_prank_ma_all=" " 20.vent_cl_prank_ma_all="95-100") ///
	xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white) 
graph save "$results\graph_`x' Subject", replace
}

*Seperate for AP for graphs to have the same scale 
foreach x in  ap_math  ap_sci ap_eng { 
*
estimates clear
eststo Maths:reghdfe `x'   ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group) cluster(sch_3)

rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all
 eststo Reading:reghdfe `x'   ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group) cluster(sch_3)

esttab using "$results/Subject_Table_`x'.csv", keep(*.vent_cl_prank_ma_all)  b(%12.3f) se(%12.3f)  nobra mtitles("Math" "Reading") replace

rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all

 coefplot (Maths, keep(*.vent_cl_prank_ma_all )  mcolor(gs7) ciopts(lcol(gs7) lwidth(vthin)) msize(vsmall) msymbol(D) ) (Reading, keep(*.vent_cl_prank_ma_all)  ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)  mcolor(black) )   ,baselevel vertical yline(0, lwidth(vthin))  xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) ysc(r(-.04 0.06)) ylabel(#6)  coeflabel(1.vent_cl_prank_ma_all="0-5"  2.vent_cl_prank_ma_all=" " 3.vent_cl_prank_ma_all=" " 4.vent_cl_prank_ma_all=" " 5.vent_cl_prank_ma_all=" " 6.vent_cl_prank_ma_all=" " 7.vent_cl_prank_ma_all=" " 8.vent_cl_prank_ma_all=" " 9.vent_cl_prank_ma_all=" " 10.vent_cl_prank_ma_all="45-50" 11.vent_cl_prank_ma_all=" " 12.vent_cl_prank_ma_all=" " 13.vent_cl_prank_ma_all=" " 14.vent_cl_prank_ma_all=" " 15.vent_cl_prank_ma_all=" " 16.vent_cl_prank_ma_all=" " 17.vent_cl_prank_ma_all=" " 18.vent_cl_prank_ma_all=" " 19.vent_cl_prank_ma_all=" " 20.vent_cl_prank_ma_all="95-100") ///
	xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white) 
graph save "$results\graph_`x' Subject", replace
}

**************************************************
*Appendix Figure 7 
**************************************************

*Seperate for AP for graphs to have the same scale 
foreach x in   STEM_major { 
*
estimates clear
eststo Maths:reghdfe `x'  ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group) cluster(sch_3)


rename st_pmrank_3_all st_prrank_3_all_temp
rename st_prrank_3_all st_pmrank_3_all
rename st_prrank_3_all_temp st_prrank_3_all


rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all

eststo Reading:reghdfe `x' ib10.vent_cl_prank_ma_all ib10.vent_cl_prank_re_all $controls if subject==1, absorb(y3_sch_coh_sub cell_ventile_ma_group cell_ventile_re_group) cluster(sch_3)

esttab using "$results/Subject_Table_`x'.csv", keep(*.vent_cl_prank_ma_all)  b(%12.3f) se(%12.3f)  nobra mtitles("Math" "Reading") replace

rename st_pmrank_3_all st_prrank_3_all_temp
rename st_prrank_3_all st_pmrank_3_all
rename st_prrank_3_all_temp st_prrank_3_all


rename vent_cl_prank_ma_all vent_cl_prank_re_temp_all
rename vent_cl_prank_re_all vent_cl_prank_ma_all
rename vent_cl_prank_re_temp_all vent_cl_prank_re_all

 coefplot (Maths, keep(*.vent_cl_prank_ma_all )  mcolor(gs7) ciopts(lcol(gs7) lwidth(vthin)) msize(vsmall) msymbol(D) ) (Reading, keep(*.vent_cl_prank_ma_all)  ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)  mcolor(black) )   ,baselevel vertical yline(0, lwidth(vthin))  xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) ysc(r(-.02 0.02)) ylabel(#6)  coeflabel(1.vent_cl_prank_ma_all="0-5"  2.vent_cl_prank_ma_all=" " 3.vent_cl_prank_ma_all=" " 4.vent_cl_prank_ma_all=" " 5.vent_cl_prank_ma_all=" " 6.vent_cl_prank_ma_all=" " 7.vent_cl_prank_ma_all=" " 8.vent_cl_prank_ma_all=" " 9.vent_cl_prank_ma_all=" " 10.vent_cl_prank_ma_all="45-50" 11.vent_cl_prank_ma_all=" " 12.vent_cl_prank_ma_all=" " 13.vent_cl_prank_ma_all=" " 14.vent_cl_prank_ma_all=" " 15.vent_cl_prank_ma_all=" " 16.vent_cl_prank_ma_all=" " 17.vent_cl_prank_ma_all=" " 18.vent_cl_prank_ma_all=" " 19.vent_cl_prank_ma_all=" " 20.vent_cl_prank_ma_all="95-100") ///
	xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white) 
graph save "$results\graph_`x' Subject", replace
}




********************************************************************************
*******************************Robustness*************************************** 
********************************************************************************


********************************************************************************
**Appendix Figure 8 Balance sample
********************************************************************************

foreach x in state_perc_8 evergrad_hs any_college ln_mean_real_wage0_1518 {
	estimates clear
	
	eststo:reghdfe `x'   cl_prank_3_all  $controls if year_3<2002, absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
		
	eststo:reghdfe `x'   ib10.vent_cl_prank_all $controls if year_3<2002 , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	estimates store `x'
	
	coefplot ,keep(*.vent_cl_prank_all )  vertical yline(0, lwidth(vthin)) ysc(r(-.04 0.06)) ylabel(#6) xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) coeflabel(1.vent_cl_prank_all="0-5"  2.vent_cl_prank_all=" " 3.vent_cl_prank_all=" " 4.vent_cl_prank_all=" " 5.vent_cl_prank_all=" " 6.vent_cl_prank_all=" " 7.vent_cl_prank_all=" " 8.vent_cl_prank_all=" " 9.vent_cl_prank_all=" " 10.vent_cl_prank_all="45-50" 11.vent_cl_prank_all=" " 12.vent_cl_prank_all=" " 13.vent_cl_prank_all=" " 14.vent_cl_prank_all=" " 15.vent_cl_prank_all=" " 16.vent_cl_prank_all=" " 17.vent_cl_prank_all=" " 18.vent_cl_prank_all=" " 19.vent_cl_prank_all=" " 20.vent_cl_prank_all="95-100")  xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white)  mcolor(black) ciopts(lcol(black) lwidth(vthin)) msize(vsmall) msymbol(D)
	graph save $results\graph_`x'_bal,  replace

	esttab using "$results/Table`x'_bal.csv", keep(cl_prank_3_all *vent_cl_prank_all) nobra b(%12.3f) se(%12.3f) mtitles("Linear" "Non Linear") replace
	}	

clear

************************************
*Appendix Figure 9 Checking functional form
************************************

 foreach x in  repeat3rd  state_perc_8  evergrad_hs  any_college enr4yr_any3years grad4in8y  ln_mean_real_wage0_1518{

	estimates clear
	
	eststo:reghdfe `x' st_prank_3_all  ib10.vent_cl_prank_all  $controls, absorb(y3_sch_coh_sub) cluster(sch_3)
	
	eststo:reghdfe `x' c.st_prank_3_all##c.st_prank_3_all   ib10.vent_cl_prank_all  $controls, absorb(y3_sch_coh_sub) cluster(sch_3)

	eststo:reghdfe `x' c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all   ib10.vent_cl_prank_all  $controls , absorb(y3_sch_coh_sub) cluster(sch_3)

	eststo:reghdfe `x' c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all   ib10.vent_cl_prank_all  $controls , absorb(y3_sch_coh_sub) cluster(sch_3)

	eststo:reghdfe `x' c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all   ib10.vent_cl_prank_all  $controls , absorb(y3_sch_coh_sub) cluster(sch_3)

	eststo:reghdfe `x' c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all   ib10.vent_cl_prank_all  $controls , absorb(y3_sch_coh_sub) cluster(sch_3)

	eststo:reghdfe `x' ib10.vent_st_prank_all  ib10.vent_cl_prank_all   $controls , absorb(y3_sch_coh_sub) cluster(sch_3)
	
	esttab using "$results/Robustness_NonLinear_Functional_Form_`x'.csv", keep(*vent_cl_prank_all) nobra b(%12.3f) se(%12.3f)  mtitles("Linear" "Quadratic" "Cubic" "Quartic" "Ventic" "Sextic" "Ventiles") replace

}

	estimates clear

********************************************************************************
*Appendix Figure 10 Conditional and Unconditional Estimates
********************************************************************************

foreach x in  repeat3rd state_perc_8 evergrad_hs any_college grad4in8y ln_mean_real_wage0_1518 {
	estimates clear
	
	eststo unconditional:reghdfe `x'  ib10.vent_cl_prank_all, absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	estimates store `x'
	
	eststo conditional:reghdfe `x'  ib10.vent_cl_prank_all $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	estimates store `x'

	coefplot (conditional, label(Conditional)  msize(vsmall) msymbol(D) )  (unconditional, label(Unconditional)  msize(vsmall) msymbol(D)),keep(*.vent_cl_prank_all )  vertical yline(0, lwidth(vthin)) ysc(r(-.04 0.06)) ylabel(#6) xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) coeflabel(1.vent_cl_prank_all="0-5"  2.vent_cl_prank_all=" " 3.vent_cl_prank_all=" " 4.vent_cl_prank_all=" " 5.vent_cl_prank_all=" " 6.vent_cl_prank_all=" " 7.vent_cl_prank_all=" " 8.vent_cl_prank_all=" " 9.vent_cl_prank_all=" " 10.vent_cl_prank_all="45-50" 11.vent_cl_prank_all=" " 12.vent_cl_prank_all=" " 13.vent_cl_prank_all=" " 14.vent_cl_prank_all=" " 15.vent_cl_prank_all=" " 16.vent_cl_prank_all=" " 17.vent_cl_prank_all=" " 18.vent_cl_prank_all=" " 19.vent_cl_prank_all=" " 20.vent_cl_prank_all="95-100")  xtitle(Third Grade Class Rank) ytitle(Impact of Rank) graphregion(color(white)) bgcolor(white) lwidth(vthin)

	graph save $results\Conditional_`x', replace
}	



********************************************************************************
*Appendix Figure 11 Small Schools
********************************************************************************

foreach x in state_perc_8 evergrad_hs any_college ln_mean_real_wage0_1518 {

	estimates clear
	
	eststo: reghdfe `x'  ib10.vent_cl_prank_all $controls if num_class_3_all<=30, absorb(i.y3_sch_coh_sub cell_school_ventile_group)  cluster(sch_3)
	
	eststo: reghdfe `x'  ib10.vent_cl_prank_all $controls if num_class_3_all<=50, absorb(i.y3_sch_coh_sub cell_school_ventile_group)  cluster(sch_3)
	
	eststo: reghdfe `x'  ib10.vent_cl_prank_all  $controls if num_class_3_all>50 & num_class_3_all<=75, absorb(i.y3_sch_coh_sub cell_school_ventile_group)   cluster(sch_3)
	
	eststo: reghdfe `x'  ib10.vent_cl_prank_all  $controls if num_class_3_all>75 & num_class_3_all<=100,absorb(i.y3_sch_coh_sub cell_school_ventile_group)   cluster(sch_3)
	
	eststo: reghdfe `x'  ib10.vent_cl_prank_all  $controls if num_class_3_all>100 & num_class_3_all<=150,absorb(i.y3_sch_coh_sub cell_school_ventile_group)   cluster(sch_3)

	esttab using "$results/Table`x'_small_comparable.csv", keep(*vent_cl_prank_all) nobra   b(%12.3f) se(%12.3f)  mtitles( "Under 30"  "Under 50" "50-75" "75-100" "100-150" ) replace
}

	
********************************************************************************
*Appendix Table 2 Deal with Draws
********************************************************************************

foreach x in state_perc_8  evergrad_hs  any_college enr4yr_any3years mean_real_wage0_1518 {

	estimates clear
	
	eststo:reghdfe `x' cl_prank_3_all $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	
	eststo:reghdfe `x' cl_prank_3_bottom $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	
	eststo:reghdfe `x' cl_prank_3_random $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)

	eststo:reghdfe `x' cl_prank_3_ontime $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
	
	esttab using "$results/Robustness_Draws_ventile_`x'", keep(cl_prank_3_*) nobra  b(%12.3f) se(%12.3f)  mtitles("All-Mean" "All-Bottom" "All-Random" "Ontime-Mean" ) replace

	estimates clear
}


********************************************************************************
*Measurement Error at end of file (Appdenix Figures 12 13 14)  
********************************************************************************


********************************************************************************
*******************************Heterogeneity************************************ 
********************************************************************************


********************************************************************************
*Appendix Figures 15/16/17 Heterogeneity by Ethnicity Gender FRPL
********************************************************************************
*Heterogenity

cap drop white
gen white=ethnic==0
replace white=. if ethnic==.

preserve
foreach y in white esl_dum fsme_dum  male  {
		
	foreach x in  state_perc_8 evergrad_hs  any_college enr4yr_any3years grad4in6y  ln_mean_real_wage0_1518{	
	
		estimates clear
		cap drop `y'_vent `y'_rank

		gen `y'_rank=`y'*cl_prank_3_all
		gen `y'_vent=`y'*vent_cl_prank_all
		
	
		if "`y'"=="male" local s "Male"
		
	
		if "`y'"=="esl_dum"	local s "ESL"
		
		if "`y'"=="fsme_dum" local s "FRSL"
		
		if "`y'"=="white" local s "White"
		
		
		eststo only_`y':reghdfe `x'  ib10.vent_cl_prank_all  if `y'==1 , absorb(y3_sch_coh_sub cell_school_ventile_group)  cluster(sch_3)
		eststo only_non_`y':reghdfe `x' ib10.vent_cl_prank_all if `y'==0 , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3)
		
		coefplot (only_`y', offset(-0.1) msize(vsmall) msymbol(D) mcolor(black) ciopts(lcol(black) lwidth(vthin) ) label(`s') ) (only_non_`y', offset(0.1)  msize(vsmall) msymbol(D) mcolor(gs7)  ciopts(lcol(gs7)  lwidth(vthin)) label(Non `s') ), keep(*.vent_cl_prank_all ) xline(9.5, lwidth(vthin) lcolor(gs4) lpattern(dash)) vertical yline(0, lwidth(vthin)) coeflabel(1.vent_cl_prank_all="0-5"  2.vent_cl_prank_all=" " 3.vent_cl_prank_all=" " 4.vent_cl_prank_all=" " 5.vent_cl_prank_all=" " 6.vent_cl_prank_all=" " 7.vent_cl_prank_all=" " 8.vent_cl_prank_all=" " 9.vent_cl_prank_all=" " 10.vent_cl_prank_all="45-50" 11.vent_cl_prank_all=" " 12.vent_cl_prank_all=" " 13.vent_cl_prank_all=" " 14.vent_cl_prank_all=" " 15.vent_cl_prank_all=" " 16.vent_cl_prank_all=" " 17.vent_cl_prank_all=" " 18.vent_cl_prank_all=" " 19.vent_cl_prank_all=" " 20.vent_cl_prank_all="95-100")  xtitle(Rank in Elementary School) ytitle(Impact of Rank) offset(0) graphregion(color(white)) bgcolor(white)

		graph save Graph "$results/Hetero Seperate `y' on `x'.gph",replace 

		esttab using "$results/Hetero_Seperate_`y'_`x'.csv", keep(*vent_cl_prank_all) nobra  b(%12.3f) se(%12.3f) mtitles("First_On" "First_Off" ) replace

		estimates clear
		}
	}

restore		


********************************************************************************
*Measurement Error**************************************************************
********************************************************************************

********************************************************************************
*Normallly Distributed ME Appendix Figure 12
********************************************************************************

clear

use $ui_dir/analysis,replace


global controls male fsme_dum esl_dum  ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5

char vent_cl_prank_ontime[omit] 10
char vent_st_prank_ontime[omit] 10

char vent_cl_prank_all[omit] 10
char vent_st_prank_all[omit] 10

char ethnic_dum[omit] 1


set seed 04062020

sum st_prank_3_all
local sd= r(sd)
di `sd'

 
foreach y in state_perc_8 evergrad_hs  any_college ln_mean_real_wage0_1518 {
foreach x in 10 20 30 {
	cap postclose buffer_`x'
	postfile buffer_`x' p_rank_noise1 p_rank_noise_se1 p_rank_noise2 p_rank_noise_se2 p_rank_noise3 p_rank_noise_se3 p_rank_noise4 p_rank_noise_se4 /// 
			p_rank_noise5 p_rank_noise_se5 p_rank_noise6 p_rank_noise_se6 p_rank_noise7 p_rank_noise_se7 p_rank_noise8 p_rank_noise_se8 p_rank_noise9 p_rank_noise_se9 ///
			p_rank_noise11 p_rank_noise_se11 p_rank_noise12 p_rank_noise_se12 p_rank_noise13 p_rank_noise_se13 p_rank_noise14 p_rank_noise_se14 p_rank_noise15 p_rank_noise_se15 ///
			p_rank_noise16 p_rank_noise_se16 p_rank_noise17 p_rank_noise_se17 p_rank_noise18 p_rank_noise_se18 p_rank_noise19 p_rank_noise_se19 p_rank_noise20 p_rank_noise_se20 using "$results\sim_ME_NL_`y'_`x'", replace
	*Set number of iterations 
	forvalues i=1/1000{
		disp "Loop `i'"
		*Proportion of test score SD that is random
		*Test scores have an SD of 
		cap drop st_noise 
		if `x'<10{
			gen st_noise=rnormal(0,`sd'*0.0`x')
		}
		else{
			gen st_noise=rnormal(0,`sd'*0.`x')
		}
		cap drop st_prank_3_noise 
		gen st_prank_3_noise=st_prank_3_all+st_noise
		sort y3_sch_coh_sub st_prank_3_noise

		cap drop cl_rank_3_noise 
		by y3_sch_coh_sub: gen cl_rank_3_noise= _n-1
		by y3_sch_coh_sub: replace cl_rank_3_noise= cl_rank_3_noise[_n-1] if st_prank_3_noise[_n]==st_prank_3_noise[_n-1] & cl_rank_3_noise[_n-1]<.
		
		cap drop cl_prank_3_noise 
		by y3_sch_coh_sub: gen cl_prank_3_noise= cl_rank_3_noise/(_N-1)

		cap drop vent_st_prank_noise
		egen vent_st_prank_noise=xtile(st_prank_3_noise), nq(20)
		char vent_st_prank_noise[omit] 10

		cap drop vent_cl_prank_noise
		egen vent_cl_prank_noise=xtile(cl_prank_3_noise), nq(20)
		char vent_cl_prank_noise[omit] 10
		
		cap drop cell_school_ventile_group
		egen cell_school_ventile_group=group(school_mean_quartile school_sd_quartile vent_st_prank_noise)
				
		reghdfe `y'  ib10.vent_cl_prank_noise  $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3) tol(0.001)
		post buffer_`x' (_b[1.vent_cl_prank_noise]) (_se[1.vent_cl_prank_noise])  (_b[2.vent_cl_prank_noise]) (_se[2.vent_cl_prank_noise])  (_b[3.vent_cl_prank_noise]) (_se[3.vent_cl_prank_noise]) ///
						(_b[4.vent_cl_prank_noise]) (_se[4.vent_cl_prank_noise])  (_b[5.vent_cl_prank_noise]) (_se[5.vent_cl_prank_noise])  (_b[6.vent_cl_prank_noise]) (_se[6.vent_cl_prank_noise]) ///
						(_b[7.vent_cl_prank_noise]) (_se[7.vent_cl_prank_noise])  (_b[8.vent_cl_prank_noise]) (_se[8.vent_cl_prank_noise])  (_b[9.vent_cl_prank_noise]) (_se[9.vent_cl_prank_noise]) ///
						(_b[11.vent_cl_prank_noise]) (_se[11.vent_cl_prank_noise])  (_b[12.vent_cl_prank_noise]) (_se[12.vent_cl_prank_noise])  (_b[13.vent_cl_prank_noise]) (_se[13.vent_cl_prank_noise]) ///
						(_b[14.vent_cl_prank_noise]) (_se[14.vent_cl_prank_noise])  (_b[15.vent_cl_prank_noise]) (_se[15.vent_cl_prank_noise])  (_b[16.vent_cl_prank_noise]) (_se[16.vent_cl_prank_noise]) ///
						(_b[17.vent_cl_prank_noise]) (_se[17.vent_cl_prank_noise])  (_b[18.vent_cl_prank_noise]) (_se[18.vent_cl_prank_noise])  (_b[19.vent_cl_prank_noise]) (_se[19.vent_cl_prank_noise]) ///
						(_b[20.vent_cl_prank_noise]) (_se[20.vent_cl_prank_noise])  
			
		drop st_noise st_prank_3_noise cl_rank_3_noise vent_st_prank_noise vent_cl_prank_noise cell_school_ventile_group
	}
	postclose buffer_`x'
}

} 

	
	
foreach y in state_perc_8 evergrad_hs  any_college  ln_mean_real_wage0_1518    {
	foreach x in 10 20 30 {
		use "$results\sim_ME_NL_`y'_`x'",replace
		di `x'
		gen error=`x'
	
		
		collapse  error p_rank_noise1-p_rank_noise_se20
		foreach i in 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20{
			gen min`i'=p_rank_noise`i'-1.96*p_rank_noise_se`i'
			gen max`i'=p_rank_noise`i'+1.96*p_rank_noise_se`i'
		}	
	
	save "$results\ME_NL_`y'_`x'_sum",replace
	}


	clear 
	use "$results\ME_NL_`y'_10_sum", replace
	foreach x in 20 30 {
		append using "$results\ME_NL_`y'_`x'_sum"
	}
	save "$results\ME_complete NL `y'",replace

	reshape long p_rank_noise p_rank_noise_se min max, i(error) j(ventile)

	twoway rcap min max ventile if error==10 , lcolor(black) || scatter p_rank_noise ventile if error==10, msymbol(D) msize(vsmall) mcolor(black) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==20 , lcolor(red) || scatter p_rank_noise ventile if error==20 , msymbol(D) msize(vsmall) mcolor(red) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==30 , lcolor(blue) || scatter p_rank_noise ventile if error==30 , msymbol(D) msize(vsmall) mcolor(blue) connect(l) lpattern(dash) ///
		xtitle("Third Grade Class Rank") ytitle("Rank Effect") legend( label( 1 "") label(2 "+10%SD error") label(3 "") label(4 "+20%SD error") label(5 "") label(6 "+30%SD error")  )

		graph save "$results\Monty Carlo Simulations NL `y'", replace
}



********************************************************************************
*Uniform Distributed ME Appendix Figure 13
********************************************************************************

clear

use $ui_dir/analysis,replace


global controls male fsme_dum esl_dum  ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5

char vent_cl_prank_ontime[omit] 10
char vent_st_prank_ontime[omit] 10

char vent_cl_prank_all[omit] 10
char vent_st_prank_all[omit] 10

char ethnic_dum[omit] 1


set seed 04062020

sum st_prank_3_all
local sd= r(sd)
di `sd'



foreach y in   state_perc_8 evergrad_hs  any_college  ln_mean_real_wage0_1518   {

foreach x in 10 20 30 {
	cap postclose buffer_`x'
	postfile buffer_`x' p_rank_noise1 p_rank_noise_se1 p_rank_noise2 p_rank_noise_se2 p_rank_noise3 p_rank_noise_se3 p_rank_noise4 p_rank_noise_se4 /// 
			p_rank_noise5 p_rank_noise_se5 p_rank_noise6 p_rank_noise_se6 p_rank_noise7 p_rank_noise_se7 p_rank_noise8 p_rank_noise_se8 p_rank_noise9 p_rank_noise_se9 ///
			p_rank_noise11 p_rank_noise_se11 p_rank_noise12 p_rank_noise_se12 p_rank_noise13 p_rank_noise_se13 p_rank_noise14 p_rank_noise_se14 p_rank_noise15 p_rank_noise_se15 ///
			p_rank_noise16 p_rank_noise_se16 p_rank_noise17 p_rank_noise_se17 p_rank_noise18 p_rank_noise_se18 p_rank_noise19 p_rank_noise_se19 p_rank_noise20 p_rank_noise_se20 using "$results\sim_ME_uni_NL_`y'_`x'", replace
	*Set number of iterations 
	forvalues i=1/1000{
		disp "Loop `i'"
		*Proportion of test score SD that is random
		*Test scores have an SD of 
		cap drop st_noise 
		if `x'<10{
			gen st_noise=runiform(-1*`sd'*0.0`x',`sd'*0.0`x')
		}
		else{
			gen st_noise=runiform(-1*`sd'*0.`x',`sd'*0.`x')
		}
		cap drop st_prank_3_noise 
		gen st_prank_3_noise=st_prank_3_all+st_noise
		sort y3_sch_coh_sub st_prank_3_noise

		cap drop cl_rank_3_noise 
		by y3_sch_coh_sub: gen cl_rank_3_noise= _n-1
		by y3_sch_coh_sub: replace cl_rank_3_noise= cl_rank_3_noise[_n-1] if st_prank_3_noise[_n]==st_prank_3_noise[_n-1] & cl_rank_3_noise[_n-1]<.
		
		cap drop cl_prank_3_noise 
		by y3_sch_coh_sub: gen cl_prank_3_noise= cl_rank_3_noise/(_N-1)

		cap drop vent_st_prank_noise
		egen vent_st_prank_noise=xtile(st_prank_3_noise), nq(20)
		char vent_st_prank_noise[omit] 10

		cap drop vent_cl_prank_noise
		egen vent_cl_prank_noise=xtile(cl_prank_3_noise), nq(20)
		char vent_cl_prank_noise[omit] 10
		
		cap drop cell_school_ventile_group
		egen cell_school_ventile_group=group(school_mean_quartile school_sd_quartile vent_st_prank_noise)
				
		reghdfe `y'  ib10.vent_cl_prank_noise  $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3) tol(0.001)
		post buffer_`x' (_b[1.vent_cl_prank_noise]) (_se[1.vent_cl_prank_noise])  (_b[2.vent_cl_prank_noise]) (_se[2.vent_cl_prank_noise])  (_b[3.vent_cl_prank_noise]) (_se[3.vent_cl_prank_noise]) ///
						(_b[4.vent_cl_prank_noise]) (_se[4.vent_cl_prank_noise])  (_b[5.vent_cl_prank_noise]) (_se[5.vent_cl_prank_noise])  (_b[6.vent_cl_prank_noise]) (_se[6.vent_cl_prank_noise]) ///
						(_b[7.vent_cl_prank_noise]) (_se[7.vent_cl_prank_noise])  (_b[8.vent_cl_prank_noise]) (_se[8.vent_cl_prank_noise])  (_b[9.vent_cl_prank_noise]) (_se[9.vent_cl_prank_noise]) ///
						(_b[11.vent_cl_prank_noise]) (_se[11.vent_cl_prank_noise])  (_b[12.vent_cl_prank_noise]) (_se[12.vent_cl_prank_noise])  (_b[13.vent_cl_prank_noise]) (_se[13.vent_cl_prank_noise]) ///
						(_b[14.vent_cl_prank_noise]) (_se[14.vent_cl_prank_noise])  (_b[15.vent_cl_prank_noise]) (_se[15.vent_cl_prank_noise])  (_b[16.vent_cl_prank_noise]) (_se[16.vent_cl_prank_noise]) ///
						(_b[17.vent_cl_prank_noise]) (_se[17.vent_cl_prank_noise])  (_b[18.vent_cl_prank_noise]) (_se[18.vent_cl_prank_noise])  (_b[19.vent_cl_prank_noise]) (_se[19.vent_cl_prank_noise]) ///
						(_b[20.vent_cl_prank_noise]) (_se[20.vent_cl_prank_noise])  
			
		drop st_noise st_prank_3_noise cl_rank_3_noise vent_st_prank_noise vent_cl_prank_noise cell_school_ventile_group
	}
	postclose buffer_`x'
}

} 

	
	
foreach y in   state_perc_8 evergrad_hs  any_college  ln_mean_real_wage0_1518   {
	foreach x in 10 20 30 {
		use "$results\sim_ME_uni_NL_`y'_`x'",replace
		di `x'
		gen error=`x'
	
		
		collapse  error p_rank_noise1-p_rank_noise_se20
		foreach i in 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20{
			gen min`i'=p_rank_noise`i'-1.96*p_rank_noise_se`i'
			gen max`i'=p_rank_noise`i'+1.96*p_rank_noise_se`i'
		}	
	
	save "$results\ME_uni_NL_`y'_`x'_sum",replace
	}


	clear 
	use "$results\ME_uni_NL_`y'_10_sum", replace
	foreach x in 20 30 {
		append using "$results\ME_uni_NL_`y'_`x'_sum"
	}
	save "$results\ME_uni_complete NL `y'",replace

	reshape long p_rank_noise p_rank_noise_se min max, i(error) j(ventile)

	twoway rcap min max ventile if error==10 , lcolor(black) || scatter p_rank_noise ventile if error==10, msymbol(D) msize(vsmall) mcolor(black) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==20 , lcolor(red) || scatter p_rank_noise ventile if error==20 , msymbol(D) msize(vsmall) mcolor(red) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==30 , lcolor(blue) || scatter p_rank_noise ventile if error==30 , msymbol(D) msize(vsmall) mcolor(blue) connect(l) lpattern(dash) ///
		xtitle("Third Grade Class Rank") ytitle("Rank Effect") legend( label( 1 "") label(2 "+10%SD error") label(3 "") label(4 "+20%SD error") label(5 "") label(6 "+30%SD error")  )

		graph save "$results\Monty Carlo Simulations NL uni `y'", replace
}




********************************************************************************
*Hetero Distributed ME Appendix Figure 14
********************************************************************************

clear 

use $ui_dir/analysis,replace


global controls male fsme_dum esl_dum  ethnic_dum2 ethnic_dum3 ethnic_dum4 ethnic_dum5

char vent_cl_prank_ontime[omit] 10
char vent_st_prank_ontime[omit] 10

char vent_cl_prank_all[omit] 10
char vent_st_prank_all[omit] 10

char ethnic_dum[omit] 1


set seed 04062020

sum st_prank_3_all
local sd= r(sd)
di `sd'


local mean= r(mean)
di `mean'

gen ab_mean_dist=abs(st_prank_3_all-`mean')*2
replace ab_mean_dist=ab_mean_dist+0.5
foreach y in state_perc_8 evergrad_hs any_college  ln_mean_real_wage0_1518  {
*   
foreach x in 10 30 20 {
	cap postclose buffer_`x'
	postfile buffer_`x' p_rank_noise1 p_rank_noise_se1 p_rank_noise2 p_rank_noise_se2 p_rank_noise3 p_rank_noise_se3 p_rank_noise4 p_rank_noise_se4 /// 
			p_rank_noise5 p_rank_noise_se5 p_rank_noise6 p_rank_noise_se6 p_rank_noise7 p_rank_noise_se7 p_rank_noise8 p_rank_noise_se8 p_rank_noise9 p_rank_noise_se9 ///
			p_rank_noise11 p_rank_noise_se11 p_rank_noise12 p_rank_noise_se12 p_rank_noise13 p_rank_noise_se13 p_rank_noise14 p_rank_noise_se14 p_rank_noise15 p_rank_noise_se15 ///
			p_rank_noise16 p_rank_noise_se16 p_rank_noise17 p_rank_noise_se17 p_rank_noise18 p_rank_noise_se18 p_rank_noise19 p_rank_noise_se19 p_rank_noise20 p_rank_noise_se20 using "$results\sim_ME_het_NL_`y'_`x'", replace
	*Set number of iterations 
	forvalues i=1/1000{
		disp "Loop `i'"
		
		cap drop st_noise 
		if `x'<10{
			gen st_noise=rnormal(0,`sd'*0.0`x')*ab_mean_dist
		}
		else{
			gen st_noise=rnormal(0,`sd'*0.`x')*ab_mean_dist
		}
		cap drop st_prank_3_noise 
		gen st_prank_3_noise=st_prank_3_all+st_noise
		sort y3_sch_coh_sub st_prank_3_noise

		cap drop cl_rank_3_noise 
		by y3_sch_coh_sub: gen cl_rank_3_noise= _n-1
		by y3_sch_coh_sub: replace cl_rank_3_noise= cl_rank_3_noise[_n-1] if st_prank_3_noise[_n]==st_prank_3_noise[_n-1] & cl_rank_3_noise[_n-1]<.
		
		cap drop cl_prank_3_noise 
		by y3_sch_coh_sub: gen cl_prank_3_noise= cl_rank_3_noise/(_N-1)

		cap drop vent_st_prank_noise
		egen vent_st_prank_noise=xtile(st_prank_3_noise), nq(20)
		char vent_st_prank_noise[omit] 10

		cap drop vent_cl_prank_noise
		egen vent_cl_prank_noise=xtile(cl_prank_3_noise), nq(20)
		char vent_cl_prank_noise[omit] 10
		
		cap drop cell_school_ventile_group
		egen cell_school_ventile_group=group(school_mean_quartile school_sd_quartile vent_st_prank_noise)
				
		reghdfe `y'  ib10.vent_cl_prank_noise  $controls , absorb(y3_sch_coh_sub cell_school_ventile_group) cluster(sch_3) tol(0.001)
		post buffer_`x' (_b[1.vent_cl_prank_noise]) (_se[1.vent_cl_prank_noise])  (_b[2.vent_cl_prank_noise]) (_se[2.vent_cl_prank_noise])  (_b[3.vent_cl_prank_noise]) (_se[3.vent_cl_prank_noise]) ///
						(_b[4.vent_cl_prank_noise]) (_se[4.vent_cl_prank_noise])  (_b[5.vent_cl_prank_noise]) (_se[5.vent_cl_prank_noise])  (_b[6.vent_cl_prank_noise]) (_se[6.vent_cl_prank_noise]) ///
						(_b[7.vent_cl_prank_noise]) (_se[7.vent_cl_prank_noise])  (_b[8.vent_cl_prank_noise]) (_se[8.vent_cl_prank_noise])  (_b[9.vent_cl_prank_noise]) (_se[9.vent_cl_prank_noise]) ///
						(_b[11.vent_cl_prank_noise]) (_se[11.vent_cl_prank_noise])  (_b[12.vent_cl_prank_noise]) (_se[12.vent_cl_prank_noise])  (_b[13.vent_cl_prank_noise]) (_se[13.vent_cl_prank_noise]) ///
						(_b[14.vent_cl_prank_noise]) (_se[14.vent_cl_prank_noise])  (_b[15.vent_cl_prank_noise]) (_se[15.vent_cl_prank_noise])  (_b[16.vent_cl_prank_noise]) (_se[16.vent_cl_prank_noise]) ///
						(_b[17.vent_cl_prank_noise]) (_se[17.vent_cl_prank_noise])  (_b[18.vent_cl_prank_noise]) (_se[18.vent_cl_prank_noise])  (_b[19.vent_cl_prank_noise]) (_se[19.vent_cl_prank_noise]) ///
						(_b[20.vent_cl_prank_noise]) (_se[20.vent_cl_prank_noise])  
			
		drop st_noise st_prank_3_noise cl_rank_3_noise vent_st_prank_noise vent_cl_prank_noise cell_school_ventile_group
	}
	postclose buffer_`x'
}

} 

foreach y in state_perc_8 evergrad_hs  any_college  ln_mean_real_wage0_1518{
* 
	foreach x in 10 20 30 {
		use "$results\sim_ME_het_NL_`y'_`x'",replace
		di `x'
		gen error=`x'
	
	
		collapse  error p_rank_noise1-p_rank_noise_se20
		foreach i in 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20{
			gen min`i'=p_rank_noise`i'-1.96*p_rank_noise_se`i'
			gen max`i'=p_rank_noise`i'+1.96*p_rank_noise_se`i'
		}	
	
	save "$results\ME_het_NL_`y'_`x'_sum",replace
	}


	clear 
	use "$results\ME_het_NL_`y'_10_sum", replace
	foreach x in 20 30 {
		append using "$results\ME_het_NL_`y'_`x'_sum"
	}
	save "$results\ME_het_complete NL `y'",replace

	reshape long p_rank_noise p_rank_noise_se min max, i(error) j(ventile)

	twoway rcap min max ventile if error==10 , lcolor(black) || scatter p_rank_noise ventile if error==10, msymbol(D) msize(vsmall) mcolor(black) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==20 , lcolor(red) || scatter p_rank_noise ventile if error==20 , msymbol(D) msize(vsmall) mcolor(red) connect(l) lpattern(dash) ///
		|| rcap min max ventile if error==30 , lcolor(blue) || scatter p_rank_noise ventile if error==30 , msymbol(D) msize(vsmall) mcolor(blue) connect(l) lpattern(dash) ///
		xtitle("Third Grade Class Rank") ytitle("Rank Effect") legend( label( 1 "") label(2 "+10%SD error") label(3 "") label(4 "+20%SD error") label(5 "") label(6 "+30%SD error")  )

		graph save "$results\Monty Carlo Simulations NL het `y'", replace
}
